﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tigera.T_Definition;

namespace Plugin.TradeSharp
{
    public partial class UCRiskSetting : UserControl
    {
        public UCRiskSetting()
        {

            FormLogin.Login();
            InitializeComponent();
            this.DataBindings.Add("Enabled", AuthUserService.Instance, "Logined", false, DataSourceUpdateMode.OnPropertyChanged);
            this.tabPage1.Text = this.ucRiskSettingRule11.Title;
            this.tabPage2.Text = this.ucRiskSettingRule21.Title;
            this.tabPage3.Text = this.ucRiskSettingRule41.Title;

            if (AuthUserService.Instance.Logined)
            {
                btn_Get_Click(this, EventArgs.Empty);
            }
            
            
        }
        public string Title
        {
            get
            {
                return "风险参数设置";
            }
        }

        private void btn_Get_Click(object sender, EventArgs e)
        {
            
            AppMgr.Instance.Mqclient.OnResponseRiskSettingInfoQuery += new EventHandler<Tigera.LibCommon.TiEventArgs<RiskSettingInfo>>(Mqclient_OnRiskSettingInfoResponse);
            TradeCMD cmd = new TradeCMD();
            cmd.Ope = TradeRiskOpe.RiskQueryRiskSettingQuery;
            AppMgr.Instance.Mqclient.SendCMD(cmd);
        }

        RiskSettingInfo _info = new RiskSettingInfo();
        void Mqclient_OnRiskSettingInfoResponse(object sender, Tigera.LibCommon.TiEventArgs<RiskSettingInfo> e)
        {
            AppMgr.Instance.Mqclient.OnResponseRiskSettingInfoQuery -= new EventHandler<Tigera.LibCommon.TiEventArgs<RiskSettingInfo>>(Mqclient_OnRiskSettingInfoResponse);

            _info = e.Data;
            this.ucRiskSettingRule11.TotalCapital = _info.TotalCapital;
            this.ucRiskSettingRule11.StockCapital = _info.StockCapital;
            this.ucRiskSettingRule11.FutureCapital = _info.FutureCapital;

            this.ucRiskSettingRule21.AllowStockSettings = _info.AllowStockSettings;

            this.ucRiskSettingRule41.AllowFutureSetting = _info.AllowFutureSetting;


         }

        private void btn_Upload_Click(object sender, EventArgs e)
        {
            _info.TotalCapital = this.ucRiskSettingRule11.TotalCapital ;
            _info.StockCapital = this.ucRiskSettingRule11.StockCapital ;
            _info.FutureCapital = this.ucRiskSettingRule11.FutureCapital ;
            _info.AllowStockSettings = this.ucRiskSettingRule21.AllowStockSettings;
            _info.AllowFutureSetting = this.ucRiskSettingRule41.AllowFutureSetting;

            T_AnyObjResponse upload = new T_AnyObjResponse();

            upload.Data = ByHelper.ES(_info);
            upload.DataType = T_AnyObjResponse.RiskQuery;
            AppMgr.Instance.Mqclient.SendDataTo(AppMgr.TradeRiskExName, ESHelper.Pack(ref upload));
            MessageBox.Show(this,"成功设置提交到服务器");
        }
    }
}
